package cn.com.coding.system.service.impl;

import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.common.utils.Query;
import cn.com.coding.system.dao.UserCardDao;
import cn.com.coding.system.domin.ProductInfoEntity;
import cn.com.coding.system.domin.UserCardEntity;
import cn.com.coding.system.domin.UserCardProductEntity;
import cn.com.coding.system.domin.dto.OrderGoodDto;
import cn.com.coding.system.domin.dto.UserCardProductDto;
import cn.com.coding.system.service.ProductInfoService;
import cn.com.coding.system.service.UserCardProductService;
import cn.com.coding.system.service.UserCardService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 购物车
 */
@Service("userCardService")
@Transactional(rollbackFor = Exception.class)
public class UserCardServiceImpl extends ServiceImpl<UserCardDao, UserCardEntity> implements UserCardService {

    @Resource
    private ProductInfoService productInfoService;

    @Resource
    private UserCardService userCardService;

    @Resource
    private UserCardProductService userCardProductService;

    @Override
    public PageUtil queryPage(Map<String, Object> params) {
        IPage<UserCardEntity> page = this.page(new Query<UserCardEntity>().getPage(params), new QueryWrapper<UserCardEntity>());

        return new PageUtil(page);
    }

    /**
     * 加入购物车
     *
     * @param userPid
     * @param productPid
     * @return
     */
    @Override
    public UserCardProductDto saveGoodCard(String userPid, String productPid) {

        ProductInfoEntity productInfoEntity = productInfoService.getById(productPid);

        UserCardEntity userCardEntity = userCardService.getOne(new QueryWrapper<UserCardEntity>().eq("user_pid", userPid).eq("card_status", 0));

        // 购物车商品信息
        UserCardProductEntity userCardGoodEntity = new UserCardProductEntity();

        userCardGoodEntity.setQuanity(1);
        userCardGoodEntity.setCardPid(userCardEntity.getPid());
        userCardGoodEntity.setPrice(productInfoEntity.getPrice());
        userCardGoodEntity.setProductPid(productInfoEntity.getPid());
        userCardGoodEntity.setProductCheck(0);
        userCardGoodEntity.setCreateTime(new Date());
        userCardGoodEntity.setUpdateTime(new Date());

        // 保存购物车商品信息
        userCardProductService.save(userCardGoodEntity);

        UserCardProductDto userCardProductDto = new UserCardProductDto();
        userCardProductDto.setProductName(productInfoEntity.getProductName());
        userCardProductDto.setCardPid(userCardEntity.getPid());
        userCardProductDto.setCreateTime(userCardGoodEntity.getCreateTime());
        userCardProductDto.setUpdateTime(userCardGoodEntity.getUpdateTime());
        userCardProductDto.setProductCheck(userCardGoodEntity.getProductCheck() == 1);
        userCardProductDto.setProductPid(userCardGoodEntity.getProductPid());
        userCardProductDto.setPid(userCardGoodEntity.getPid());
        userCardProductDto.setQuanity(userCardGoodEntity.getQuanity());
        userCardProductDto.setPrice(userCardGoodEntity.getPrice());


        return userCardProductDto;
    }

    /**
     * 生成订单
     *
     * @param orderGoodDto
     */
    @Override
    public void addOrder(OrderGoodDto orderGoodDto) {
        UserCardEntity userCardEntity = userCardService.getOne(new QueryWrapper<UserCardEntity>().eq("user_pid", orderGoodDto.getUserPid()).eq("card_status", 0));

        List<UserCardProductEntity> oldGood = userCardProductService.getCardProductByCardPid(userCardEntity.getPid());
        BigDecimal priceNum = new BigDecimal("0.00");
        if (orderGoodDto.getGoodList().size() < oldGood.size()) {
            UserCardEntity newOrder = new UserCardEntity();
            newOrder.setUserPid(orderGoodDto.getUserPid());
            newOrder.setCardStatus(1);
            userCardService.save(newOrder);

            orderGoodDto.getGoodList().forEach(e -> {
                UserCardProductEntity byId = userCardProductService.getById(e.getPid());
                byId.setProductCheck(1);
                byId.setCardPid(newOrder.getPid());
                newOrder.setPrice(priceNum.add(byId.getPrice()));
                userCardProductService.saveOrUpdate(byId);
            });

            newOrder.setUpdateTime(new Date());

            userCardService.saveOrUpdate(newOrder);


        } else {
            // 设置为选中 意为完成订单
            orderGoodDto.getGoodList().forEach(e -> {
                UserCardProductEntity byId = userCardProductService.getById(e.getPid());
                byId.setProductCheck(1);
                userCardEntity.setPrice(priceNum.add(byId.getPrice()));
                userCardProductService.saveOrUpdate(byId);
            });

            // 生成订单
            userCardEntity.setCardStatus(1);
            userCardEntity.setUpdateTime(new Date());
            userCardService.saveOrUpdate(userCardEntity);

            // 创建购物车
            UserCardEntity newUserCardEntity = new UserCardEntity();
            newUserCardEntity.setUserPid(orderGoodDto.getUserPid());
            newUserCardEntity.setCardStatus(0);
            userCardService.save(newUserCardEntity);
        }

    }

    /**
     * 判断商品是否存在
     *
     * @param userPid
     * @param productPid
     * @return
     */
    @Override
    public boolean goodExist(String userPid, String productPid) {

        // 获取用户购物车
        UserCardEntity userCardEntity = userCardService.getOne(new QueryWrapper<UserCardEntity>().eq("user_pid", userPid).eq("card_status", 0));

        // 查询是否存在
        UserCardProductEntity userCardGoodEntity = userCardProductService.getOne(new QueryWrapper<UserCardProductEntity>().eq("card_pid", userCardEntity.getPid()).eq("product_pid", productPid).eq("product_check", 0));

        // 不存在返回 false
        if (userCardGoodEntity == null) return false;

        ProductInfoEntity good = productInfoService.getById(productPid);
        // 若存在仅仅更新下它的数量即可
        userCardGoodEntity.setQuanity(userCardGoodEntity.getQuanity() + 1);
        userCardGoodEntity.setPrice(userCardGoodEntity.getPrice().add(good.getPrice()));

        // 保存
        userCardProductService.saveOrUpdate(userCardGoodEntity);

        // exist
        return true;
    }

}